package com.kun.controller;

import com.kun.entity.ResponseJson;
import com.kun.entity.SuppInfo;


import com.kun.service.SuppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supp")
public class SuppController {
    @Autowired
    private SuppInfoService suppInfoService;
    @GetMapping("/supp/{suppId}")
    public ResponseJson<SuppInfo> findById(@PathVariable("suppId")Integer suppId){
        SuppInfo suppInfo =suppInfoService.findById(suppId);
        ResponseJson<SuppInfo> result = new ResponseJson<>();
        if(suppInfo == null){
            result.setCode(500);
            result.setMsg("未找到数据");
        }else{
            result.setCode(200);
            result.setMsg("找到数据");
            result.setData(suppInfo);
        }
        return result;
    }
    @GetMapping("/findByPage")
    public ResponseJson<Map> findByPages(@RequestParam("page") Integer pageNum
            , @RequestParam("limit") Integer pageSize
            , SuppInfo suppInfo){
        Map<String, Object> data = suppInfoService.selectByPage(pageNum, pageSize,suppInfo);
        ResponseJson<Map> result = new ResponseJson<>();
        if (data != null){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(data);
        }else {
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }
    @PostMapping("/insert")
    public ResponseJson<SuppInfo> insertSupp(SuppInfo suppInfo){
        ResponseJson<SuppInfo> result = new ResponseJson<>();
        int ui =suppInfoService.insertSupp(suppInfo);
        if (ui == 0){
            result.setCode(500);
            result.setMsg("添加失败");
        }
        result.setCode(200);
        result.setMsg("添加成功");
        return result;
    }
    @DeleteMapping("/batchDelete")
    public ResponseJson batchDelete(Integer[] suppIds){
        ResponseJson<SuppInfo> result = new ResponseJson<>();
        if (suppInfoService.batchDelete(suppIds)>0){
            result.setCode(200);
            result.setMsg("删除成功");
        }else{
            result.setCode(500);
            result.setMsg("删除失败");
        }
        return result;
    }
    @PutMapping("/updateSupp")
    public ResponseJson updateSupp(SuppInfo suppInfo){
        ResponseJson<SuppInfo> result = new ResponseJson<>();
        if (suppInfoService.updateUser(suppInfo)>0){
            result.setCode(200);
            result.setMsg("更新成功");
        }else{
            result.setCode(500);
            result.setMsg("更新失败");
        }
        return result;
    }

}
